/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View;

import Utilities.TrainingUtilities;
import com.sun.lwuit.Button;
import com.sun.lwuit.ButtonGroup;
import com.sun.lwuit.Command;
import com.sun.lwuit.Dialog;
import com.sun.lwuit.Display;
import com.sun.lwuit.Label;
import com.sun.lwuit.RadioButton;
import com.sun.lwuit.TextField;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import testmenu.MainMidlet;

/**
 *
 * @author Windsea
 */
public class settingForm extends View {

    private final Label lbname;
    private final Label lbage;
    private final Label lbweight;
    private final Label lbheight;
    private final Label lbgender;
    private final Label lbbmi;
    private final TextField txName;
    private final TextField txage;
    private final TextField txweight;
    private final RadioButton male;
    private final RadioButton female;
    private final TextField txheight;
    private final Button btncalc;
    private final Label lbbmistt;
    private final Command cmdBack;
    private final Label lbwant;
    private final RadioButton dew;
    private final RadioButton inw;
ButtonGroup btwant;
    private final RadioButton miw;
    public settingForm(final MainMidlet midlet) {
        super(midlet);
        lbname = new Label("Tên");
        lbage = new Label("Tuổi");
        lbweight = new Label("Cân nặng (kg)");
        lbheight = new Label("Chiều cao (cm)");
        lbgender = new Label("Giới tính");

        lbbmi = new Label();
        lbbmistt = new Label();
        lbwant = new Label("Bạn muốn?");
        txName = new TextField();
        txage = new TextField();
        txage.setConstraint(TextField.NUMERIC);
        txweight = new TextField();
        txweight.setConstraint(TextField.NUMERIC);
        txheight = new TextField();
        txheight.setConstraint(TextField.NUMERIC);
        male = new RadioButton("Nam");
        female = new RadioButton("Nữ");
        ButtonGroup btgender = new ButtonGroup();
        dew = new RadioButton("Giảm cân");
        miw = new RadioButton("Giữ nguyên");
        inw = new RadioButton("Tăng cân");
        btwant = new ButtonGroup();
        btwant.add(dew);
        btwant.add(inw);
        btwant.add(miw);
        btgender.add(male);
        btgender.add(female);
        
        btncalc = new Button("Tính toán");
        btncalc.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                try{
                TrainingUtilities tu = new TrainingUtilities();
                double weight = Double.parseDouble(txweight.getText());
                double height = Double.parseDouble(txheight.getText());
                double bminum = tu.calc_BMI(weight, height);
                System.out.println("BMI number: " + String.valueOf(bminum));
                lbbmi.setText("Chỉ số BMI: " + String.valueOf(bminum));
                String bmiString = tu.BMI_Status(bminum);
                lbbmistt.setText("Tình trạng: " + bmiString);
                midlet.setVar_Yourname(txName.getText());
                midlet.setVar_Age(Integer.parseInt(txage.getText()));
                midlet.setVar_Weight(weight);
                midlet.setVar_Height(height);
                midlet.setVar_BMI(bminum);
                midlet.setVar_WeightMode(btwant.getSelectedIndex());
                boolean gender = false;
                if (male.isSelected()) {
                    gender = true;
                } else {
                    gender = false;
                }
                midlet.setVar_Gender(gender);
                midlet.saveUserData();

                removeAll();
                addComponent(lbbmi);
                addComponent(lbbmistt);
                addCommand(cmdBack);
                }
                catch(Exception e)
                {
                    Dialog.show("Lỗi", "Lỗi định dạng !", "OK", null);
                }
            }
        });
        cmdBack = new Command("") {
            public void actionPerformed(ActionEvent evt) {
                Display.getInstance().setForceFullScreen(true);
                midlet.getmmf().showBack();
                Display.getInstance().getCurrent().refreshTheme();
            }
        };
        addComponent(lbname);
        addComponent(txName);
        addComponent(lbage);
        addComponent(txage);
        addComponent(lbweight);
        addComponent(txweight);
        addComponent(lbheight);
        addComponent(txheight);
        addComponent(lbgender);
        addComponent(male);
        addComponent(female);
        addComponent(lbwant);
        addComponent(dew);
        addComponent(miw);
        addComponent(inw);
        addComponent(btncalc);
        addCommand(cmdBack);
        setBackCommand(cmdBack);


    }
}
